package 剑指offer.demo4;

import com.sun.jndi.ldap.Connection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

//https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993?
/*
使用sort重新定义排序方式
 */
public class 把数组排成最小的数 {
    public String PrintMinNumber(int [] numbers) {
        //合法性判断
        if (numbers.length == 0){
            return new String();
        }
        //定一个动态数组方便比较里面的值并把所有numbers的值添加到arr
        ArrayList<Integer> arr = new ArrayList<>();
        for (int a : numbers
             ) {
            arr.add(a);
        }
        //对list进行排序
        Collections.sort(arr, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                String s1 = o1 + "" + o2;
                String s2 = o2 + "" + o1;
                return s1.compareTo(s2);
            }
        });
        //把arr添加到String
        String str = new String();
        for (int a : arr
             ) {
            str += a;
        }
        return str;
    }
}
